﻿package com.mttang.utils
{
     /**
	 * @弹出框;
	 * @Author  fox
	 * @Version 1.0
	 * @Modification
	 */	
	import flash.display.Bitmap
	import flash.display.BitmapData;
	import flash.display.GradientType;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.BitmapFilter;
	import flash.filters.BlurFilter;
	import flash.filters.BitmapFilterQuality;
	import flash.filters.DropShadowFilter;
	import flash.filters.GlowFilter;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.display.DisplayObject;
	import flash.system.System;

	//
	public class PopupWindow
	{
		private static var stage:Stage=null;
		private static var WindowObject:Sprite = new Sprite();
		
		//初始化，让popupwindow对象在舞台的最上方显示;
		public static function init(stageReference:Stage):void
		{
			stage=stageReference;
		}

		// close windows
		public static function close():void
		{
			//stage.removeChildAt(stage.numChildren - 1);
			//stage.removeChild(window);
			var mywindow:Sprite = stage.getChildByName("window") as Sprite;
			stage.removeChild(mywindow);
			mywindow = null;
		}

		//需要显示的对象;colour为背景颜色;
		public static function show(objectDisplay:DisplayObject,CallFunction:Function=null,Colour:int=0xB0A89B):void
		{
			if (stage == null)
			{
				trace("PopupWindow class has not been initialised!");
				return;
			}
			var window:Sprite = new Sprite();
			window.name = "window";
			window.addChild(createBackground(Colour));
			objectDisplay.x=(stage.stageWidth - objectDisplay.width) * 0.5;
			objectDisplay.y=(stage.stageHeight - objectDisplay.height) * 0.5;
			window.addChild(objectDisplay);
			stage.addChildAt(window, stage.numChildren);
			if (CallFunction != null)
			{
			CallFunction();
			}
		}

		private static function createBackground(Colour:int):Sprite
		{
			//创建一个具有指定的宽度和高度的 BitmapData 对象。
			//var BackgroundBD:BitmapData=new BitmapData(stage.stageWidth, stage.stageHeight, true, 0xFF000000 + Colour);
			var BackgroundBD:BitmapData=new BitmapData(stage.stageWidth, stage.stageHeight, true, 0xFF000000);
			//
			//var stageBackground:BitmapData=new BitmapData(stage.stageWidth, stage.stageHeight);
			//
			//stageBackground.draw(stage);
			//
			//var rect:Rectangle=new Rectangle(0, 0, stage.stageWidth, stage.stageHeight);
			//
			//var point:Point=new Point(0, 0);
			//
			//var multiplier:uint=200;
			//对每个通道执行从源图像向目标图像的混合
			//BackgroundBD.merge(stageBackground, rect, point, multiplier, multiplier, multiplier, multiplier);
			//取得一个源图像和一个滤镜对象，并生成过滤的图像。
			//BackgroundBD.applyFilter(BackgroundBD, rect, point, new BlurFilter(2, 2));
			//
			var bitmap:Bitmap=new Bitmap(BackgroundBD);
			//
			var Background:Sprite = new Sprite();
			Background.alpha = 0.5;
			Background.addChild(bitmap);
			return Background;
		}
	}
}